<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/uploadExcel')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="isShowDialog = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" border :data="list" :default-sort="{prop: 'workNumber'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="scope">
              <!-- 插值表达式支持函数执行 并且渲染的是函数的return值 -->
              {{ scope.row.formOfEmployment == 1 ? '正式':'非正式' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" prop="enableState">
            <template #default="scope">
              <!-- 插值表达式支持函数执行 并且渲染的是函数的return值 -->
              {{ scope.row.enableState == 1 ? '正常':'非正常' }}
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" fixed="right" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="goDetail(scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="editRole(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDelEmployee(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :total="total"
            :current-page="params.page"
            :page-size="params.size"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <add-employees :show-dialog="isShowDialog" @close-dialog="closeDialog" />
    <assign-role :show-role-dialog.sync="showRoleDialog" :cur-id="curId" @close-role-dialog="closeRoleDialog" />
  </div>
</template>

<script>
import { reqGetEmployeeList, delEmployee } from '@/api/employees'
import addEmployees from './components/addEmployees.vue'
import assignRole from './components/assignRole.vue'
import dayjs from 'dayjs'
export default {
  name: 'Employees',
  components: {
    addEmployees,
    assignRole
  },
  data() {
    return {
      showRoleDialog: false,
      curId: '',
      isShowDialog: false,
      list: [],
      total: 0,
      loading: false,
      params: {
        page: 1,
        size: 10
      }
    }
  },
  mounted() {
    this.renderEmployeeList()
  },
  methods: {
    async renderEmployeeList() {
      this.loading = true
      const res = await reqGetEmployeeList(this.params)
      // console.log(res)
      const { total, rows } = res
      this.total = total
      this.list = rows
      this.loading = false
    },
    formatDate(value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.renderEmployeeList()
    },
    async hDelEmployee(id) {
      this.$confirm('你确认要删除么?', '温馨提示').then(async() => {
        await delEmployee(id)
        this.renderEmployeeList()
        this.$message.success('删除员工成功')
      }).catch(error => {
        console.log(error)
      })
    },
    closeDialog() {
      this.isShowDialog = false
    },
    goDetail(id) {
      this.$router.push({
        path: '/employees/detail',
        query: {
          id
        }
      })
    },
    editRole(id) {
      console.log(id)
      this.showRoleDialog = true
      this.curId = id
    },
    closeRoleDialog() {
      this.showRoleDialog = false
    }
  }
}
</script>
